//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using CommunityServer.Files.Components;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CA = ComponentArt.Web.UI;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;

namespace CommunityServer.ControlPanel.FileAdmin
{
	/// <summary>
	///	Summary description for DefaultPermissionListControl.
	/// </summary>
	public class DefaultPermissionListControl : BaseFilesPermissionsGridControl
	{
		#region Child Controls

		protected ResourceLinkButton resetPermissions;
		protected ResourceLinkButton SaveButton;

		#endregion

		private void Page_Load(object sender, EventArgs e)
		{
			resetPermissions.Attributes.Add("onclick", string.Format("return confirm('{0}');", CommunityServer.ControlPanel.Components.ResourceManager.GetString("CP_FilesAdmin_DefaultPermissionsList_ResetPermission_Warning")));
			this.SectionID = -1;
		}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
			this.Grid.DeleteCommand += new CA.Grid.GridItemEventHandler(this.Grid_DeleteCommand);
			this.resetPermissions.Click += new EventHandler(this.resetPermissions_Click);
			this.SaveButton.Click +=new EventHandler(Save_Click);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new EventHandler(this.Page_Load);

		}
		#endregion


		private void Grid_DeleteCommand(object sender, CA.GridItemEventArgs e)
		{
			SetDefaultPermissions(Roles.GetRole(new Guid(e.Item["RoleIDString"].ToString())));
		}

		private void resetPermissions_Click(object sender, EventArgs e)
		{
			if(!CSContext.Current.User.IsAdministrator)
				throw new CSException(CSExceptionType.AdministrationAccessDenied);

			foreach(Role r in Roles.GetRoles())
				SetDefaultPermissions(r);

			//Seems like a CA Bug, but our checkboxes will loose their values if we postback without re-binding the data
			buildGrid();
			Grid.DataBind();
		}

		private void SetDefaultPermissions(Role r)
		{
			FileGalleryPermission p = null;
			bool isNew = false;
			p = FileGalleryPermissions.GetFolderPermission(-1, r.RoleID);
					
			if(p == null)
			{
				isNew = true;
				p = new FileGalleryPermission();
				p.SectionID = -1;
				p.RoleID = r.RoleID;
			}

			//Reset the permissions
			p.ApplicationType = ApplicationType.FileGallery;
			p.AllowMask = Permission.Undefined;
			p.DenyMask =  Permission.Undefined;

			if(r.Name == Roles.FilesAdministrator)
			{
				p.SetBit(Permission.SystemAdmin, AccessControlEntry.Deny);
				p.SetBit(Permission.Administer, AccessControlEntry.Allow);
				p.SetBit(Permission.Moderate, AccessControlEntry.Allow);
				p.SetBit(Permission.Vote, AccessControlEntry.Allow);
				p.SetBit(Permission.Delete, AccessControlEntry.Allow);
				p.SetBit(Permission.Edit, AccessControlEntry.Allow);
				p.SetBit(Permission.Reply, AccessControlEntry.Allow);
				p.SetBit(Permission.Post, AccessControlEntry.Allow);
				p.SetBit(Permission.View, AccessControlEntry.Allow);
			}
			else if(r.Name == Roles.Editor)
			{
				p.SetBit(Permission.SystemAdmin, AccessControlEntry.Deny);
				p.SetBit(Permission.Administer, AccessControlEntry.Deny);
				p.SetBit(Permission.Moderate, AccessControlEntry.Deny);
				p.SetBit(Permission.Vote, AccessControlEntry.Allow);
				p.SetBit(Permission.Delete, AccessControlEntry.Allow);
				p.SetBit(Permission.Edit, AccessControlEntry.Allow);
				p.SetBit(Permission.Reply, AccessControlEntry.Allow);
				p.SetBit(Permission.Post, AccessControlEntry.Allow);
				p.SetBit(Permission.View, AccessControlEntry.Allow);
			}
			else if(r.Name == Roles.Everyone)
			{
				p.SetBit(Permission.SystemAdmin, AccessControlEntry.Deny);
				p.SetBit(Permission.Administer, AccessControlEntry.Deny);
				p.SetBit(Permission.Moderate, AccessControlEntry.Deny);
				p.SetBit(Permission.Vote, AccessControlEntry.Allow);
				p.SetBit(Permission.Delete, AccessControlEntry.Deny);
				p.SetBit(Permission.Edit, AccessControlEntry.Deny);
				p.SetBit(Permission.Reply, AccessControlEntry.Allow);
				p.SetBit(Permission.Post, AccessControlEntry.Deny);
				p.SetBit(Permission.View, AccessControlEntry.Allow);
			}
			else if (r.Name == Roles.ForumsAdministrator)
			{
				p.SetBit(Permission.SystemAdmin, AccessControlEntry.Deny);
				p.SetBit(Permission.Administer, AccessControlEntry.Allow);
				p.SetBit(Permission.Moderate, AccessControlEntry.Allow);
				p.SetBit(Permission.Vote, AccessControlEntry.Allow);
				p.SetBit(Permission.Delete, AccessControlEntry.Allow);
				p.SetBit(Permission.Edit, AccessControlEntry.Allow);
				p.SetBit(Permission.Reply, AccessControlEntry.Allow);
				p.SetBit(Permission.Post, AccessControlEntry.Allow);
				p.SetBit(Permission.View, AccessControlEntry.Allow);
			}
			else if (r.Name == Roles.GalleryAdministrator)
			{
				p.SetBit(Permission.SystemAdmin, AccessControlEntry.Deny);
				p.SetBit(Permission.Administer, AccessControlEntry.Allow);
				p.SetBit(Permission.Moderate, AccessControlEntry.Allow);
				p.SetBit(Permission.Vote, AccessControlEntry.Allow);
				p.SetBit(Permission.Delete, AccessControlEntry.Allow);
				p.SetBit(Permission.Edit, AccessControlEntry.Allow);
				p.SetBit(Permission.Reply, AccessControlEntry.Allow);
				p.SetBit(Permission.Post, AccessControlEntry.Allow);
				p.SetBit(Permission.View, AccessControlEntry.Allow);
			}
			else if (r.Name == Roles.Moderator)
			{
				p.SetBit(Permission.SystemAdmin, AccessControlEntry.Deny);
				p.SetBit(Permission.Administer, AccessControlEntry.Deny);
				p.SetBit(Permission.Moderate, AccessControlEntry.Allow);
				p.SetBit(Permission.Vote, AccessControlEntry.Allow);
				p.SetBit(Permission.Delete, AccessControlEntry.Allow);
				p.SetBit(Permission.Edit, AccessControlEntry.Allow);
				p.SetBit(Permission.Reply, AccessControlEntry.Allow);
				p.SetBit(Permission.Post, AccessControlEntry.Allow);
				p.SetBit(Permission.View, AccessControlEntry.Allow);
			}
			else if (r.Name == Roles.RegisteredUsers)
			{
				p.SetBit(Permission.SystemAdmin, AccessControlEntry.Deny);
				p.SetBit(Permission.Administer, AccessControlEntry.Deny);
				p.SetBit(Permission.Moderate, AccessControlEntry.Deny);
				p.SetBit(Permission.Vote, AccessControlEntry.Allow);
				p.SetBit(Permission.Delete, AccessControlEntry.Allow);
				p.SetBit(Permission.Edit, AccessControlEntry.Allow);
				p.SetBit(Permission.Reply, AccessControlEntry.Allow);
				p.SetBit(Permission.Post, AccessControlEntry.Deny);
				p.SetBit(Permission.View, AccessControlEntry.Allow);
			}
			else if (r.Name == Roles.SystemAdministrator)
			{
				p.SetBit(Permission.SystemAdmin, AccessControlEntry.Allow);
				p.SetBit(Permission.Administer, AccessControlEntry.Allow);
				p.SetBit(Permission.Moderate, AccessControlEntry.Allow);
				p.SetBit(Permission.Vote, AccessControlEntry.Allow);
				p.SetBit(Permission.Delete, AccessControlEntry.Allow);
				p.SetBit(Permission.Edit, AccessControlEntry.Allow);
				p.SetBit(Permission.Reply, AccessControlEntry.Allow);
				p.SetBit(Permission.Post, AccessControlEntry.Allow);
				p.SetBit(Permission.View, AccessControlEntry.Allow);
			}
			else
			{
				p.SetBit(Permission.SystemAdmin, AccessControlEntry.Deny);
				p.SetBit(Permission.Administer, AccessControlEntry.Deny);
				p.SetBit(Permission.Moderate, AccessControlEntry.Deny);
				p.SetBit(Permission.Vote, AccessControlEntry.Deny);
				p.SetBit(Permission.Delete, AccessControlEntry.Deny);
				p.SetBit(Permission.Edit, AccessControlEntry.Deny);
				p.SetBit(Permission.Reply, AccessControlEntry.Deny);
				p.SetBit(Permission.Post, AccessControlEntry.Deny);
				p.SetBit(Permission.View, AccessControlEntry.Deny);
			}
		
			SavePermission(p, isNew);
		}

		private void Save_Click(object sender, EventArgs e)
		{
			this.Save();

			CommunityServer.Files.Components.Folders.GetFolders(false, true);

			Response.Redirect(Request.RawUrl);
		}

		protected override bool CreateFromDefault(PermissionBase permission)
		{
			return false;
		}

		protected override bool IsNew(PermissionBase permission)
		{
			return permission == null;
		}



	}
}
